<%@ taglib prefix="page" uri="http://com.qipay/pagelib" %>
<%@ page language="java" import="com.qipay.admin.utils.SiteHelper" contentType="text/html;charset=utf-8" %>
<%@ page import="com.qipay.baas.model.CpAccount" %>
<%@ page import="com.qipay.configuration.config.ConfigurationEnum" %>
<%@ page import="com.qipay.user.entity.UserLevel" %>
<%@ page import="org.apache.commons.collections.CollectionUtils" %>
<%@ page import="org.apache.commons.lang3.StringUtils" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.Map" %>
<%@ page import="com.qipay.admin.withdraw.vo.*" %>
<!doctype html>
<html>
<head>
    <jsp:include page="head.jsp"></jsp:include>
    <title>提现申请</title>
    <link rel="stylesheet" href="<%= request.getAttribute("StaticHost") %>/css/dataTables.bootstrap.min.css">
    <link rel="stylesheet" href="<%= request.getAttribute("StaticHost") %>/css/bootstrapValidator.min.css">
    <script src="<%= request.getAttribute("StaticHost") %>/js/jquery.js"></script>

</head>
<body class="devpreview hold-transition skin-blue sidebar-mini">
<div class="wrapper">
    <jsp:include page="nav.jsp"></jsp:include>
    <jsp:include page="left.jsp"></jsp:include>
    <div class="content-wrapper">
        <section class="content">
            <div class="row">
                <div class="col-xs-12">
                    <div class="box border" >
                        <%
                            UserLevel userLevel = (UserLevel) request.getAttribute("level");
                        %>
                        <div class="" id="searchForm">
                            <div class="">
                                <div class="col-sm-12 col-md-12">
                                    <div class="box-header">
                                        <h3 class="box-title">银行卡选择</h3>
                                    </div>
                                    <div class="tab-content">
                                        <div class="tab-pane active" id="tab_withdraw">
                                            <div class="form-group">
                                                <select id="cardBankList" class="form-control">
                                                    <%
                                                        List<BindVo> cardList = (List<BindVo>) request.getAttribute("cardList");
                                                        if (CollectionUtils.isNotEmpty(cardList)) {
                                                            for (BindVo p : cardList) {
                                                    %>
                                                    <option bank="<%=p.getCardBank()%>"
                                                            subBank="<%=p.getCardSubBank()%>"
                                                            no="<%=p.getCardNo()%>"
                                                            name="<%=p.getName()%>"
                                                            mobile="<%=p.getMobile()%>">
                                                        <%=p.getCardBank() + " - " + p.getCardSubBank() + " - "+p.getName()+" - " + p.getCardNo()%>
                                                    </option>
                                                    <%
                                                            }
                                                        }
                                                    %>
                                                </select>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                                <div class="col-sm-12 col-md-12">
                                    <div class="box-header">
                                        <h3 class="box-title">余额信息</h3>
                                    </div>
                                    <%
                                        CpAccount account = (CpAccount) request.getAttribute("account");
                                        Map<Byte, String> conf = (Map<Byte, String>) request.getAttribute("config");
                                        boolean isHoliday = (boolean) request.getAttribute("isHoliday");
                                    %>
                                    <form class="form-horizontal">
                                        <div class="form-group col-sm-4 col-md-4 col-lg-4">
                                            <b>账户可用余额</b>
                                            <input type="text" class="border" id="available" readonly
                                                   style="background-color: #f8f8f8;"
                                                         value="<%=SiteHelper.formatMoney(account.getBalanceAvailable(), 100.0)%>">
                                            <input type="hidden" id="availableVal"
                                                   value="<%=SiteHelper.formatNumber(account.getBalanceAvailable(), 100.0)%>">
                                        </div>
                                        <div class="form-group col-sm-4 col-md-4 col-lg-4">
                                            <b>账户总余额</b>
                                            <input type="text" class="border" id="balance" readonly
                                                   style="background-color: #f8f8f8;"
                                                   value="<%= SiteHelper.formatMoney(account.getBalance(), 100.0)%>">

                                        </div>
                                        <div class="form-group col-sm-4 col-md-4 col-lg-4" >
                                            <b>账户不用余额</b>
                                            <input type="text" class="border" id="disabled" readonly
                                                   style="background-color: #f8f8f8;"
                                                   value="<%=SiteHelper.formatMoney(account.getBalanceDisabled(), 100.0)%>">
                                            <input type="hidden" id="disabledVal"
                                                   value="<%=SiteHelper.formatNumber(account.getBalanceDisabled(), 100.0)%>">

                                        </div>

                                        <label hidden for="min" class="col-sm-5 col-md-5 col-lg-5">最小金额</label>
                                        <div hidden class="form-group col-sm-7 col-md-7 col-lg-7">
                                            <input type="text" class="form-control" id="min"readonly
                                                   value="<%=SiteHelper.formatMoney(conf.get(ConfigurationEnum.WITHDRAW_MIN.getType()))%>">
                                            <input type="hidden" id="minVal"
                                                   value="<%=SiteHelper.formatNumber(conf.get(ConfigurationEnum.WITHDRAW_MIN.getType()))%>">
                                        </div>
                                        <label hidden for="max" class="col-sm-5 col-md-5 col-lg-5">最大金额</label>
                                        <div hidden class="form-group col-sm-7 col-md-7 col-lg-7">
                                            <input type="text" class="form-control" id="max" readonly
                                                   value="<%=SiteHelper.formatMoney(isHoliday? conf.get(ConfigurationEnum.WITHDRAW_MAX_HOLIDAY.getType()): conf.get(ConfigurationEnum.WITHDRAW_MAX_WORK.getType()))%>">
                                            <input type="hidden" id="maxVal"
                                                   value="<%=
                                                   SiteHelper.formatNumber(
                                                           isHoliday
                                                    ? conf.get(ConfigurationEnum.WITHDRAW_MAX_HOLIDAY.getType())
                                                    : conf.get(ConfigurationEnum.WITHDRAW_MAX_WORK.getType()))%>">
                                        </div>
                                        <label hidden for="cost" class="col-sm-5 col-md-5 col-lg-5">可用余额-固定收费</label>
                                        <div hidden class="form-group col-sm-7 col-md-7 col-lg-7"><input type="text"
                                                                                                         class="form-control"
                                                                                                         id="cost"
                                                                                                         readonly
                                                                                                         value="<%=SiteHelper.formatMoney(conf.get(ConfigurationEnum.WITHDRAW_AVAILABLE_COST.getType()))%>">
                                            <input type="hidden" id="costVal"
                                                   value="<%=SiteHelper.formatNumber(conf.get(ConfigurationEnum.WITHDRAW_AVAILABLE_COST.getType()))%>">
                                        </div>
                                        <input type="hidden" id="password"
                                               value="<%=request.getAttribute("password")%>">
                                        <input type="hidden" id="start"
                                               value="<%=conf.get(ConfigurationEnum.AUDIT_START_TIME.getType())%>">
                                        <input type="hidden" id="end"
                                               value="<%=conf.get(ConfigurationEnum.AUDIT_END_TIME.getType())%>">
                                    </form>

                                </div>
                            </div>
                            <div style="clear: both;"></div>
                        </div>
                        <div class=" " style="border-top: 0px solid #dcdcdc;padding-top: 10px;">
                            <%
                                WithdrawVo detail = (WithdrawVo) request.getAttribute("detail");
                                String validCode = (String) request.getAttribute("validCode");
                            %>
                            <div class="">
                                <form class="form-horizontal" action="${pageContext.request.contextPath}/withdraw" method="POST" id="applyForm">
                                    <input type="hidden" class="form-control" id="id" name="id"
                                           value="<%=detail.getId()%>">
                                    <input type="hidden" class="form-control" id="operation"
                                           name="operation" value="<%=detail.getOperation()%>">
                                    <input type="hidden" class="form-control" id="type"
                                           name="type" value="<%=detail.getType()%>">

                                    <label class="col-sm-2" for="amount">金额</label>
                                    <div class="form-group col-sm-10"><input type="text" class="form-control"
                                                                             id="amount" name="amount" onchange="calc()"
                                                                             value="<%=null!= detail.getAmount()? Double.valueOf(detail.getAmount()): 0%>">
                                    </div>
                                    <label class="col-sm-2" for="fee">手续费</label>
                                    <div class="form-group col-sm-10">
                                        <input type="text" class="form-control " id="fee" name="fee" readonly
                                               style="background-color: #f8f8f8"
                                                value="<%= null!= detail.getFee()?detail.getFee(): 0%>">
                                    </div>
                                    <label class="col-sm-2" for="actualAmount">到账金额</label>
                                    <div class="form-group col-sm-10">
                                        <input type="text" class="form-control " readonly id="actualAmount"
                                               style="background-color: #f8f8f8"
                                                name="actualAmount" value="<%=null!= detail.getActualAmount()? Double.valueOf(detail.getActualAmount()): 0%>">
                                    </div>

                                    <%--<label class="col-sm-2" >支行所在省</label>
                                    <div class="form-group col-sm-10">
                                        <select id="province" class="form-control" name="province">
                                        </select>
                                    </div>

                                    <label class="col-sm-2">支行所在市</label>
                                    <div class="form-group col-sm-10">
                                        <select id="city" class="form-control" onchange="findSunBank();" name="city">
                                        </select>
                                    </div>
                                    <div class="form-group col-sm-10" style="display: none">
                                        <select id="county"></select>
                                    </div>--%>

                                    <label class="col-sm-2" >银行名称</label>
                                    <div class="form-group col-sm-10">
                                        <select id="cardBank" name="cardBank" onchange="findSunBank();" class="form-control">
                                                <option value="招商银行">招商银行</option>
                                                <option value="中国工商银行">中国工商银行</option>
                                                <option value="中国建设银行">中国建设银行</option>
                                                <option value="浦发银行">浦发银行</option>
                                                <option value="中国农业银行">中国农业银行</option>
                                                <option value="中国民生银行">中国民生银行</option>
                                                <option value="平安银行">平安银行</option>
                                                <option value="兴业银行">兴业银行</option>
                                                <option value="交通银行">交通银行</option>
                                                <option value="中信银行">中信银行</option>
                                                <option value="中国光大银行">中国光大银行</option>
                                                <option value="华夏银行">华夏银行</option>
                                                <option value="中国银行">中国银行</option>
                                                <option value="广发银行">广发银行</option>
                                                <option value="邮政储蓄银行">邮政储蓄银行</option>
                                                <option value="其他银行">其他银行</option>
                                        </select>
                                    </div>

                                    <label class="col-sm-2">支行名称</label>
                                    <div class="form-group col-sm-10">
                                        <%--<select id="cardSubBank" name="cardSubBank" class="form-control">
                                        </select>--%>
                                        <input style="display:inline-block;width:50%;" type="text" class="form-control"
                                               id="cardSubBank"
                                               name="cardSubBank"
                                               placeholder="支行名称"
                                               value=""><span style="color:red;font-size: 20px;">*谨慎填写分行名称，填写错误会导致无法到账*</span>
                                    </div>
                                    <label class="col-sm-2" for="cardNo">银行卡号</label>
                                    <div class="form-group col-sm-10"><input type="text" class="form-control"
                                                                             id="cardNo"
                                                                             name="cardNo"
                                                                             placeholder="银行卡号"
                                                                             value="<%=StringUtils.isNotBlank(detail.getCardNo())?detail.getCardNo(): ""%>">
                                    </div>
                                    <label class="col-sm-2" for="name">开户名</label>
                                    <div class="form-group col-sm-10"><input type="text" class="form-control"
                                                                             id="name"
                                                                             name="name"
                                                                             placeholder="开户人姓名"
                                                                             value="<%=StringUtils.isNotBlank(detail.getName())?detail.getName(): ""%>">
                                    </div>
                                    <label class="col-sm-2" for="withdrawPassword">操作密码</label>
                                    <div class="form-group col-sm-10"><input type="text" class="form-control"
                                                                             id="withdrawPassword"
                                                                             name="withdrawPassword"
                                                                             value="">
                                    </div>

                                    <div class="form-group  col-sm-10" style="display: none">
                                        <input type="text" class="form-control"
                                               id="validCode"
                                               name="validCode"
                                               value="<%=validCode%>">
                                    </div>

                                    <div class="box-footer">
                                        <button type="button" class="btn btn-primary btn-sm borderNone" onclick="window.history.back()">
                                            返回
                                        </button>
                                        <button type="submit" class="btn btn-success btn-sm borderNone" id="btnSubmit">提交</button>

                                    </div>
                                </form>
                            </div>
                        </div>
                    </div>
                </div>
    </div>
    </section>

</div>
<%--<jsp:include page="foot.jsp"></jsp:include>--%>
</div>

<script src="<%= request.getAttribute("StaticHost") %>/js/adminlte.min.js"></script>
<script src="<%= request.getAttribute("StaticHost") %>/js/icheck.min.js"></script>
<script src="<%= request.getAttribute("StaticHost") %>/js/bootstrapValidator.min.js"></script>
<script src="<%= request.getAttribute("StaticHost") %>/js/business/withdraw_apply.js?v=<%= request.getAttribute("StaticVersion") %>"></script>
<script type="text/javascript">
    $(function () {
        // init.args
        var operation = 0;
        var password = $("#password").val();
        var min = Number($("#minVal").val());
        var max = Number($("#maxVal").val());
        console.log(min);
        console.log($("#maxVal").val() + "= " + max);
        var start = $("#start").val();
        var end = $("#end").val();
        var code = "";
        type = 0;

        // init.fill
        $("#type").val(type);
        $("#operation").val(operation);
        $("#operationName").val($("#operation_tab li.active a").text());
        initAmount();

        // init.validator
        $('#applyForm').bootstrapValidator({
            framework: 'bootstrap',
            icon: {
                valid: 'glyphicon glyphicon-ok',
                invalid: 'glyphicon glyphicon-remove',
                validating: 'glyphicon glyphicon-refresh'
            },
            fields: {
                operationName: {
                    validators: {
                        notEmpty: {
                            message: '操作类型不能为空'
                        }, callback: {
                            message: '提现申请时间为 ' + start + '-' + end + ',请至有效时间后刷新界面重试',
                            callback: function (value, validator) {
                                return checkTime();
                            }
                        }
                    }
                },
                amount: {
                    validators: {
                        notEmpty: {
                            message: '操作金额不能为空'
                        },
                        callback: {
                            message: '操作金额不足',
                            callback: function (value, validator) {
                                var available = Number($("#availableVal").val());
                                return value <= available;
                            }
                        },
                        between: {
                            min: min,
                            max: max,
                            message: '操作金额范围为' + min + '到' + max
                        }
                    }
                },
                fee: {
                    validators: {
                        notEmpty: {
                            message: '扣费金额不能为空'
                        }
                    }
                },
                actualAmount: {
                    validators: {
                        notEmpty: {
                            message: '实际到账金额不能为空'
                        }
                    }
                },
                cardBank: {
                    validators: {
                        notEmpty: {
                            message: '银行名称不能为空'
                        }
                    }
                },
                cardSubBank: {
                    validators: {
                        notEmpty: {
                            message: '支行名称不能为空'
                        }
                    }
                },
                cardNo: {
                    validators: {
                        notEmpty: {
                            message: '卡号不能为空'
                        }, stringLength: {
                            min: 16,
                            max: 20,
                            message: '银行卡位数有误'
                        },
                    }
                },
                idCardNo: {
                    validators: {
                        notEmpty: {
                            message: '卡号不能为空'
                        }, stringLength: {
                            min: 17,
                            max: 18,
                            message: '身份证位数有误'
                        },
                    }
                },
                name: {
                    validators: {
                        notEmpty: {
                            message: '姓名不能为空'
                        }
                    }
                },
                // validCode: {
                //     validators: {
                //         notEmpty: {
                //             message: '手机校验码不能为空'
                //         },
                //         callback: {
                //             message: '手机校验码错误',
                //             callback: function (value, validator) {
                //                 return code == value;
                //             }
                //         },
                //     }
                // },
                withdrawPassword: {
                    validators: {
                        notEmpty: {
                            message: '操作密码不能为空'
                        },
                        callback: {
                            message: '操作密码错误',
                            callback: function (value, validator) {
                                return password == value;
                            }
                        },
                    },
                    regexp: {
                        regexp: /^[0-9]{6}$/,
                        message: '操作密码位数有误'
                    }
                },
            }
        });

        // sms
        $("#sendValid").click(function () {
            //看是否在两分钟之内，有没有发送验证码，如果有，则拒绝发送---服务端查询。
            $.ajax({
                url: "${pageContext.request.contextPath}/withdraw/queryValid",
                type: "PUT",
                async: true,
                context: this,
                timeout: 50000,
                success: function (data) {
                    if (null != data && data.code == 200) {
                        var limit = 60;
                        $("#sendValid").attr("disabled", true);
                        var timer = setInterval(function () {
                            if (--limit != 0) {
                                console.log(limit)
                                $("#sendValid").text(limit + "秒后重发");
                            } else {
                                console.log("60s pass")
                                $("#sendValid").text("发送校验码");
                                $("#sendValid").attr("disabled", false);
                                clearInterval(timer);
                            }
                        }, 1000);
                        // request
                        $.get("${pageContext.request.contextPath}/withdraw/valid", function (data) {
                            if (null != data && data.code == 200) {
                                code = data.data
                            } else {
                                console.log("get fail")
                                clearInterval(timer);
                                $("#sendValid").attr("disabled", false);
                                $("#sendValid").text("发送校验码");
                                alert("失败:" + data.msg);
                            }
                        }).fail(function () {
                            console.log("get request fail")
                            clearInterval(timer);
                            $("#sendValid").attr("disabled", false);
                            $("#sendValid").text("发送校验码");
                        });
                    } else {
                        alert("操作过于频繁，提现操作限定为2分钟一次噢");
                    }
                },
                error: function (d, s, b) {
                    //alert("失败:" + b);
                    $('#banlance').val("0");
                }
            });


        });
    });

    function checkTime() {
        // curTime
        var date = new Date();
        var start = $("#start").val();
        var end = $("#end").val();
        var curTime = date.getHours() * 100 + date.getMinutes();
        var startLimit = parseFloat(start.replace(":", ""));
        var endLimit = parseFloat(end.replace(":", ""));
        // check
        return curTime > startLimit && curTime < endLimit
    }

</script>
</body>
</html>

